package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.NocvData;
import com.example.entity.NocvNews;
import com.example.service.NocvNewsService;
import com.example.vo.DataView;
import com.example.vo.NocvNewsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/news")
public class NocvNewsController {

    @Autowired
    private NocvNewsService nocvNewsService;

    @RequestMapping("/listNews")
    public DataView listNews(NocvNewsVo nocvNewsVo){
        IPage<NocvNews> page=new Page<>(nocvNewsVo.getPage(), nocvNewsVo.getLimit());
        QueryWrapper<NocvNews> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(nocvNewsVo.getTitle()),"title",nocvNewsVo.getTitle());
        nocvNewsService.page(page,queryWrapper);
        DataView dataView=new DataView(page.getTotal(),page.getRecords());
        return dataView;
    }


    @RequestMapping("/deleteById")
    public DataView deleteById(Integer id){
        nocvNewsService.removeById(id);
        DataView dataview=new DataView();
        dataview.setCode(200);
        dataview.setMsg("删除数据成功");
        return dataview;
    }

    @RequestMapping("/addOrUpdateXueYuan")
    public DataView addOrUpdateNews(NocvNews nocvNews){
        nocvNews.setCreateTime(new Date());
        boolean save = nocvNewsService.saveOrUpdate(nocvNews);
        DataView dataview=new DataView();
        if(save){
            dataview.setCode(200);
            dataview.setMsg("新增数据成功");
            return dataview;
        }
        dataview.setCode(100);
        dataview.setMsg("操作失败");
        return dataview;
    }
}
